package com.example.controller;

import com.example.pojo.Qualification;
import com.example.pojo.ResultBean;
import com.example.req.QualificationTaskReq;
import com.example.service.IQualificationService;
import com.example.service.QualificationProcessService;
import com.example.vo.QualificationVo;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/qualificationProcess")
public class QualificationProcessController {
    @Autowired
    QualificationProcessService qualificationProcessService;
    @Autowired
    IQualificationService qualificationService;

    /**
     * 开启资质审核流程
     * @param qualification
     * @return
     */
    @PostMapping("startQualification")
    @RequiresPermissions({"company:detail","company:accounts"})
    public ResultBean startQualification(@RequestBody Qualification qualification) {
        System.out.println("资质信息" + qualification);
        boolean rs = qualificationProcessService.startQualification(qualification);
        return rs ? ResultBean.success() : ResultBean.error();
    }

    /**
     * 根据UserId查询资质审核任务
     * @param id
     * @return
     */
    @GetMapping("queryQualificationTaskByUserId/{id}")
    public ResultBean queryQualificationTaskByUserId(@PathVariable("id") Long id) {
        List<Task> tasks = qualificationProcessService.queryQualificationTaskByUserId(id);
        List<Map<String, Object>> rs = new ArrayList<>();
        for (Task task : tasks) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", task.getId());
            map.put("name", task.getName());
            map.put("createTime", task.getCreateTime());
            rs.add(map);
        }
        return ResultBean.success(rs);
    }

    //根据taskId获取task任务相关的表单信息，包括当前任务可执行的命令、关联的申请单数据以及关联的批注列表
    @GetMapping("/viewTaskForm/{taskId}")
    public ResultBean viewTaskForm(@PathVariable("taskId") String taskId) {
        System.out.println("请求的taskID：" + taskId);
        //获取唯一标识businessKey，从而判断当前任务属于哪个申请流程
        String businessKey = qualificationProcessService.getBusinessKeyByTaskId(taskId);
        String qualificationId = businessKey.split("\\.")[1];
        Map<String, Object> map = new HashMap<>();

        //获取资质单信息
        QualificationVo qualificationVo = qualificationService.queryById(qualificationId);
        map.put("qualificationInfo", qualificationVo);

        //获取关联的批注信息列表
        List<Comment> comments = qualificationProcessService.queryAllCommentByTaskId(taskId);
        //获取当前任务可执行的命令
        List<String> buttonList = qualificationProcessService.queryButtonListByTaskId(taskId);

        map.put("comments", comments);
        map.put("buttonList", buttonList);

        return ResultBean.success(map);
    }

    /**
     * 流程推进
     * @param qualificationTaskReq
     * @return
     */
    @PostMapping("/complete")
    public ResultBean complete(@RequestBody QualificationTaskReq qualificationTaskReq){
        System.out.println("资质审核流程推进的数据:" + qualificationTaskReq);
        boolean rs = qualificationProcessService.complete(qualificationTaskReq);

        return rs ? ResultBean.success() : ResultBean.error();
    }
}
